home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-20 / rs0422.zip / ROSEZSW / LOADER.C < prev    next >
C/C++ Source or Header  |  1990-11-15  |  12KB  |  436 lines

  1. /*
  2.  * Copyright 1988 by the Radio Amateur Telecommunications Society
  3.  * and Thomas A. Moulton, W2VY
  4.  *
  5.  * This software may only be modified, copied, distributed or
  6.  * executed for non-profit purposes by individuals operating
  7.  * systems in the Amateur Radio Service.  Credit to the
  8.  * author(s) and to the Radio Amateur Telecommunications Society
  9.  * must be made in modules where RATS provided software is used,
  10.  * and in any announcements and documentation.  
  11.  *
  12.  * As a non-profit, research and development organization,  the
  13.  * Radio Amateur Telecommunications Society distributes software
  14.  * in both executable and source forms.  This policy is in place
  15.  * to encourage the development and distribution of OSI-based,
  16.  * networking tools.  In order to protect the interests of the
  17.  * Society and the authors, we have placed some conditions
  18.  * of use on the software.  Other groups are encouraged
  19.  * to place the same or similar guidelines on
  20.  * software they produce.
  21.  *
  22.  * The Radio Amateur Telecommunications Society reserves the right
  23.  * to specify and alter the terms under which software provided by
  24.  * the Society may be used.  This policy is consistent with the 
  25.  * objective of uniform and consistent "Open Systems Interconnections."
  26.  * 
  27.  * All acceptable Amateur Radio related uses of this software
  28.  * will be outlined in the "ROSE Implementer's Guide".  Individuals
  29.  * or organizations wishing to add to, or modify the provisions of
  30.  * the guide to accommodate local or evolutionary requirements
  31.  * should document the proposed change(s) and forward them to the
  32.  * Society.  If accepted, written notification will be provided by
  33.  * the Society to the submitting organization or individual(s).
  34.  * The Society will then issue a "ROSE Implementer's Guide Change
  35.  * Notice".  Periodically, the Society will re-issue the "ROSE 
  36.  * Implementer's Guide" and incorporate the text of the change 
  37.  * notices.  This procedure has been put in to place to ensure
  38.  * compatibility between systems and to ensure their "Openness"
  39.  * and interoperability.
  40.  *
  41.  * No part of this software may be used in other packages 
  42.  * without prior authorization from the author or the Society.  
  43.  * Software incorporating this module, all or in part, must be 
  44.  * provided to the Society prior to distribution or use by
  45.  * anyone not directly involved in testing of the revised  
  46.  * environment.  Current releases of the combined software must
  47.  * be provided to the Society in both source and executable
  48.  * forms.  Adequate documention to produce an executable module 
  49.  * from the provided source must also be included.
  50.  *
  51.  * Non-Amateur Radio non-profit uses may be authorized on a case
  52.  * by case basis.  Inquiries for such use may be made in writing
  53.  * to the Society. Non-commercial uses consistent with the
  54.  * general principles of Open Systems Interconnection Reference
  55.  * Model will be generally considered with favor.
  56.  *
  57.  * Commercial licensing of the software is also available based
  58.  * on normal commercial terms.  Licensing inquiries should be
  59.  * directed to the Society.  Commercial licensing of the standard
  60.  * software will be done in situations which materially benefit
  61.  * the Amateur Radio Packet Network.  Additional licensing is
  62.  * reserved by the individual authors.
  63.  * 
  64.  * The Radio Amateur Telecommunications Society provides this software
  65.  * on an "as is" basis.  The Society assumes no liability for
  66.  * loss incurred through the use of this software.  Amateur Radio
  67.  * use of this software implies non-commercial and voluntary 
  68.  * development, deployment and use of this software in a "Amateur",
  69.  * non-commercial service.  Commercial users are encouraged to
  70.  * inspect their copies of the source code.  Source code modification
  71.  * licenses are available if a combined Object and Source Code
  72.  * license was not originally established.
  73.  * 
  74.  * The Society may be contacted by writing or calling at:
  75.  * 
  76.  * The Radio Amateur Telecommunications Society 
  77.  * 206 North Vivyen Street.
  78.  * Bergenfield, New Jersey 07621
  79.  *
  80.  * Telephone: 201-387-8896
  81.  *
  82.  */
  83. #include "data.h"
  84. #include "buffer.h"
  85. #include "iface.h"
  86. #include "timer.h"
  87. #include "ax25.h"
  88. #include "ax25l2.h"
  89. #include "l3struc.h"
  90. #include "x25cause.h"
  91. #include "l3calls.h"
  92. #include "tx.h"
  93. #include "upfcn.h"
  94.  
  95. extern struct datastr *l2_user_info, l2_info;
  96. extern int maxfcn;
  97. extern struct upfcn *spcfcn[];
  98.  
  99. #define bt_clrbas() free_stuff(&ld_membase)
  100. #define bt_clrdta() free_stuff(&save_bt_data)
  101.  
  102. struct datastr *mkpkt();
  103. unsigned char *malloc(), *calloc();
  104. void free();
  105. int passgen(), passvfy();
  106.  
  107. int rst_boot(), clr_boot(), con_boot(), recv_boot(), send_boot();
  108. int NULLPKT(), NULLFCN();
  109.  
  110. struct upfcn loader={
  111.     {{0x98,0x9e,0x82,0x88,0x8a,0xa4}, {0x00}}, /* LOADER-0 */
  112.     rst_boot, clr_boot, con_boot, recv_boot, send_boot, NULLFCN, NULLPKT,
  113.     "LOADER - Program Interface Ver 1.1\r"
  114. };
  115.  
  116. char Entry[]="Entry #? ";
  117. char entry[10];
  118.  
  119. char bt_busy;
  120. char bt_state;
  121. struct datastr *bt_in;
  122. char bt_cmd;
  123. char bt_obj;
  124. int bt_len;
  125. int bt_pos;
  126. unsigned char *bt_data, *save_bt_data;
  127. unsigned char bt_checksum;
  128. char bt_high;
  129. unsigned char bt_chv;
  130. int ld_memsiz, ld_relen;
  131. unsigned int *ld_membase;
  132. unsigned char passkey[16];
  133.  
  134. int
  135. hexch(ch)
  136. unsigned char ch;
  137. {
  138.     if (ch>0x39) ch -= 7;
  139.     if (ch<0x30 || ch>0x3f) return -1;
  140.     return (ch-0x30);
  141. }
  142.  
  143. int
  144. xch(val)
  145. unsigned char val;
  146. {
  147.     val = (val & 0x0f);
  148.     if (val > 9) val += 7;
  149.     return (val + 0x30);
  150. }
  151.  
  152. putxch(p,val)
  153. register struct datastr *p;
  154. int val;
  155. {
  156.     bappch(p,xch(val >> 4));
  157.     bappch(p,xch(val));
  158. }
  159.  
  160. putxint(p,val)
  161. register struct datastr *p;
  162. int val;
  163. {
  164.     putxch(p,val);        /* Low byte Low address */
  165.     putxch(p,(val>>8));
  166. }
  167.  
  168. struct datastr *
  169. info_pkt()
  170. {
  171.     return (dup_pkt(&l2_info));
  172. }
  173.  
  174. int
  175. con_boot(vcx)
  176. struct VCS *vcx;
  177. {
  178.     register struct VCS *vcp;
  179.     static struct VCS *vc;
  180.  
  181.     vc=vcx;
  182.     vcp=vc->peer;
  183.     if (!vcp) return;
  184.     puthex2("con",vc,"at",fn);
  185.     send_l3(vcp,info_pkt());
  186.     if (bt_busy) vc_clear(vc, Number_Busy);
  187.     else {
  188.         bt_busy=1;
  189.         bt_state = passgen(vcp,passkey);
  190.     }
  191. }
  192.  
  193. int
  194. clr_boot(vc,c)
  195. struct VCS *vc;
  196. int c;
  197. {
  198.     bt_clrbas();
  199.     bt_clrdta();
  200.     bt_busy=0;
  201.     set_p(vc, P1, 0);
  202. }
  203.  
  204. int
  205. recv_boot(vc,pkt)
  206. register struct VCS *vc;
  207. struct datastr *pkt;
  208. {
  209.     if (vc) send_l3(vc->peer,NULL);    /* Kick transmitter */
  210. /*    if (vc) if (vc=vc->peer) (*vc->SEND)(vc,NULL);    /* Kick transmitter */
  211. }
  212.  
  213. int
  214. rst_boot(vc)
  215. register struct VCS *vc;
  216. {
  217.     set_d(vc, D1, 0);
  218.     vc=vc->peer;
  219. /*    if (vc) /* Check for Peer */
  220.     send_l3(vc, mkpkt("\rRESET\r"));
  221. }
  222.  
  223. int
  224. unload_ok()
  225. {
  226.     return 1;
  227. }
  228.  
  229. bt_is_ok(vc)
  230. struct VCS *vc;
  231. {
  232.     send_l3(vc,mkpkt("OK\r"));
  233. }
  234.  
  235. int
  236. vc_clear0(vc)
  237. struct VCS *vc;
  238. {
  239. /*    vc_clear(vc, DTE_Orig); */
  240.     set_p(vc, P6, DTE_Orig);
  241. }
  242.  
  243. int
  244. send_boot(vc, pkt)
  245. register struct VCS *vc;
  246. struct datastr *pkt;
  247. {
  248.     static int i, ch;
  249.  
  250.     puthex2("send fig",vc,"",pkt);
  251.     vc_queue_data(vc,pkt);
  252.     while ((bt_in = vc->tx_queue)) /* There is stuff waiting */ {
  253.         vc->tx_queue=bt_in->next;
  254.         bt_in->next=NULL;
  255.         if (bt_state == 0) /* Check for correct Password */ {
  256. #if 0
  257.             bt_state++;
  258. #else
  259.             if (passvfy(&bt_in, passkey)) {
  260.                 bt_state=1;
  261.                 bt_is_ok(vc->peer);
  262.             } else /* Dump this Turkey */ {
  263.                 queue(NULL,vc_clear0,0,vc);
  264.                 return 0;
  265.             }
  266. #endif
  267.         }
  268.         if (bt_in) /* We have more in this packet */ {
  269.             while ((ch=bgetch(&bt_in)) != EOF) {
  270.                 puthex2("loop",ch,"",bt_in);
  271.                 if (ch > 0x20) /* Allow Control chars to pass */ {
  272.                     i=hexch(ch);
  273.                     if (ch == 0x3a || i<0 ) bt_state=1;
  274.                     if (bt_state == 1) /* Clean up */ {
  275.                         bt_clrdta();
  276.                         bt_data = NULL;
  277.                         bt_chv = ch;
  278.                         bt_high = TRUE;
  279.                     } else {
  280.                         if (bt_high) bt_chv = i<<4;
  281.                         else bt_chv += i;
  282.                         bt_high = !bt_high;
  283.                     } 
  284.                     if (bt_high) bt_next(vc, bt_chv);
  285.                 }
  286.             }
  287.         }
  288.     }
  289.     return 256;    /* We are never busy! */
  290. }
  291.  
  292. /* bt_state has the following meaning 
  293. /*    1 - Searching for a : (Start of record)
  294. /*    2 - Next byte read is the command
  295. /*    3 - Next byte read is the object we are working with
  296. /*    4 - Next byte read is the low byte of length !Low byte Low address!
  297. /*    5 - Next byte read is the high byte of length
  298. /*    6 - Next byte read is additional data
  299. /*    7 - Processing request
  300. */
  301.  
  302. #define MAXCMD 3
  303. #define MAXOBJ 15
  304.  
  305. bt_next(vc,chv)
  306. struct VCS *vc;
  307. unsigned char chv;
  308. {
  309.     static char err;
  310.     static int len;
  311.     register struct VCS *vcp;
  312.     int do_boot();
  313.  
  314.     err=0;
  315.     vcp=vc->peer;
  316.     puthex2("next",bt_state,"ch",chv);
  317.     switch (bt_state) {
  318.         case 1: if (chv != 0x3a) bt_state=0;
  319.             break;
  320.         case 2: bt_cmd=chv;
  321.             if (chv > MAXCMD) err=1;
  322.             break;
  323.         case 3: bt_obj=chv;
  324.             break;
  325.         case 4: bt_len= chv;    /* Low byte Low address */
  326.             break;
  327.         case 5: bt_len += (chv<<8);
  328.             bt_pos=0;
  329.             bt_checksum=0;
  330.             if (bt_cmd == 1 && bt_obj == 0x42) /* Code */ {
  331.                 if (ld_memsiz < bt_len) err = 9;
  332.                 else len = ld_memsiz;
  333.             } else len=bt_len;
  334.             if (len) {
  335.                 bt_data=save_bt_data=calloc(1,len);
  336.                 if (bt_data == NULL) err = 3;
  337.             }
  338.             if (bt_len == 0) bt_state++; /* No data field */
  339.             break;
  340.         case 6: bt_data[bt_pos]=chv;
  341.             bt_pos++;
  342.             bt_checksum += chv;
  343.             if (bt_pos != bt_len) bt_state--;
  344.             break;
  345.         case 7: bt_checksum += chv;
  346.             if (bt_checksum != 0) err=4;
  347.             else err=do_boot(vcp);
  348.             if (!err) bt_is_ok(vcp);
  349.             bt_clrdta();
  350.             bt_state=0;
  351.             break;
  352.     }
  353.     if (err) {
  354.         if (vcp) send_l3(vcp,l_message(" Error ", err));
  355.         bt_state=0;
  356.         bt_clrbas();
  357.     }
  358.     bt_state++;
  359.     puthex2("exit",bt_state,"err",err);
  360. }
  361.  
  362. int
  363. do_boot(vc)
  364. struct VCS *vc;
  365. {
  366.     static int i;
  367.     static struct datastr *bp;
  368.     static unsigned int *rel;
  369.     int bt_usr_inf();
  370.  
  371.     if (bt_cmd == 0) /* Print list */ {
  372.         ld_memsiz = ld_relen = 0;
  373.         bp = new_buffer(256);
  374.         for (i=0;i<maxfcn;i++) {
  375.             entry[7] = ('0' + (i>9 ? i+7 : i));
  376.             bappstr(bp, entry);
  377.             bp = bappstr(bp, spcfcn[i]->title);
  378.         }
  379.         send_l3(vc,bp);
  380.     } else if (bt_cmd == 1) /* Load request */ {
  381.         if (bt_obj == 0) /* Load User Info Text Message */ {
  382.             i=bt_usr_inf();
  383.             sumchk();
  384.             return i;
  385.         } else
  386.         if (bt_obj == 0x41) /* Sizing info */ {
  387. #ifndef z80
  388.             return 15;
  389. #endif
  390.             if (maxfcn >= 15) return 14;
  391.             ld_memsiz = *(unsigned int *)bt_data;
  392.             bt_data += 2;    /* Skip an Int */
  393.             ld_relen = *(unsigned int *)bt_data;
  394.         } else if (bt_obj == 0x42) /* Code Segment */ {
  395.             if (maxfcn >= 15) return 14;
  396.             bt_clrbas();
  397.             ld_membase = (unsigned int *)bt_data;
  398.             save_bt_data = NULL;    /* Don't free the memory */
  399.         } else if (bt_obj == 0x43) /* Relocation data */ {
  400.             if (bt_len != ld_relen*2 || ld_membase == NULL
  401.                 || bt_data == NULL) return 13;
  402.             rel = (unsigned int *)bt_data;
  403.             for (i=0;i<ld_relen;i++, rel++) /* Relocate an address */ {
  404.                 *(int *)((int)ld_membase + (int)*rel) += (unsigned int)ld_membase;
  405.             }
  406.             spcfcn[maxfcn]=(struct upfcn *)ld_membase;
  407.             ld_membase = NULL;
  408.             (*spcfcn[maxfcn]->init)(); /* Init it, if needed */
  409.             maxfcn++;    /* It is now valid */
  410.         } else return 15;
  411.     } else if (bt_cmd == 2) /* Delete application */ {
  412.         ld_memsiz = ld_relen = 0;
  413.         if (bt_obj < 0 || bt_obj >= maxfcn || maxfcn == 1) return 12;
  414.         if ((*spcfcn[bt_obj]->unload)()) /* Unloaded OK */ {
  415.             free(spcfcn[bt_obj]);
  416.             maxfcn--;
  417.             /* Move others up */
  418.             for (i=bt_obj;i<maxfcn;i++) spcfcn[i]=spcfcn[i+1];
  419.             spcfcn[maxfcn+1]=NULL;
  420.         }
  421.     } else return 16;
  422.     return 0;
  423. }
  424.  
  425. bt_usr_inf()
  426. {
  427.     register struct datastr *bp;
  428.  
  429.     if (l2_user_info != &l2_info) free_pkt(l2_user_info);
  430.     l2_user_info=info_pkt();
  431.     if (bt_len) if (!(bp=new_buffer(bt_len))) return 3;
  432.     while (bt_len--) bappch(bp, *bt_data++);
  433.     l2_user_info=binsert(l2_user_info,bp);
  434.     return 0;
  435. }
  436.